﻿Imports System
Imports System.Globalization
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Design

Namespace System.Workflow.Activities.Rules
    Friend NotInheritable Class RuleConditionReferenceValidator
        Inherits ConditionValidator
        ' Methods
        Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
            If (manager Is Nothing) Then
                Throw New ArgumentNullException("manager")
            End If
            If (manager.Context Is Nothing) Then
                Throw New InvalidOperationException(Messages.ContextStackMissing)
            End If
            Dim errors As ValidationErrorCollection = MyBase.Validate(manager, obj)
            Dim reference As RuleConditionReference = [TryCast](obj,RuleConditionReference)
            If (reference Is Nothing) Then
                Throw New ArgumentException(String.Format(CultureInfo.CurrentCulture, Messages.UnexpectedArgumentType, New Object() { GetType(RuleConditionReference).FullName, "obj" }), "obj")
            End If
            Dim activity As Activity = [TryCast](manager.Context.Item(GetType(Activity)),Activity)
            If (activity Is Nothing) Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, New Object() { GetType(Activity).Name }))
            End If
            If Not TypeOf manager.Context.Item(GetType(PropertyValidationContext)) Is PropertyValidationContext Then
                Throw New InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Messages.ContextStackItemMissing, New Object() { GetType(PropertyValidationContext).Name }))
            End If
            If Not String.IsNullOrEmpty(reference.ConditionName) Then
                Dim definitions As RuleDefinitions = Nothing
                Dim conditions As RuleConditionCollection = Nothing
                Dim declaringActivity As CompositeActivity = Helpers.GetDeclaringActivity(activity)
                If (declaringActivity Is Nothing) Then
                    declaringActivity = [TryCast](Helpers.GetRootActivity(activity),CompositeActivity)
                End If
                If (Not activity.Site Is Nothing) Then
                    definitions = ConditionHelper.Load_Rules_DT(activity.Site, declaringActivity)
                Else
                    definitions = ConditionHelper.Load_Rules_RT(declaringActivity)
                End If
                If (Not definitions Is Nothing) Then
                    conditions = definitions.Conditions
                End If
                If ((conditions Is Nothing) OrElse Not conditions.Contains(reference.ConditionName)) Then
                    Dim error As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.ConditionNotFound, New Object() { reference.ConditionName }), &H53E) { _
                        .PropertyName = (MyBase.GetFullPropertyName(manager) & ".ConditionName") _
                    }
                    errors.Add([error])
                    Return errors
                End If
                Dim condition As RuleCondition = conditions.Item(reference.ConditionName)
                Dim typeProvider As ITypeProvider = DirectCast(manager.GetService(GetType(ITypeProvider)), ITypeProvider)
                Dim checkStaticType As Boolean = False
                Dim service As IWorkflowCompilerOptionsService = [TryCast](manager.GetService(GetType(IWorkflowCompilerOptionsService)),IWorkflowCompilerOptionsService)
                If ((Not service Is Nothing) AndAlso service.CheckTypes) Then
                    checkStaticType = True
                End If
                Dim validation As New RuleValidation(activity, typeProvider, checkStaticType)
                condition.Validate(validation)
                Dim errors2 As ValidationErrorCollection = validation.Errors
                If (errors2.Count > 0) Then
                    Dim fullPropertyName As String = MyBase.GetFullPropertyName(manager)
                    Dim errorText As String = String.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionExpression, New Object() { fullPropertyName })
                    Dim errorNumber As Integer = &H558
                    If (Not activity.Site Is Nothing) Then
                        Dim error2 As New ValidationError(errorText, errorNumber) { _
                            .PropertyName = (fullPropertyName & ".Expression") _
                        }
                        errors.Add(error2)
                    Else
                        Dim error3 As ValidationError
                        For Each error3 In errors2
                            Dim error4 As New ValidationError((errorText & " " & error3.ErrorText), errorNumber) { _
                                .PropertyName = (fullPropertyName & ".Expression") _
                            }
                            errors.Add(error4)
                        Next
                    End If
                End If
                Dim condition2 As RuleCondition
                For Each condition2 In conditions
                    If ((condition2.Name = reference.ConditionName) AndAlso (Not condition2 Is condition)) Then
                        Dim error5 As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.DuplicateConditions, New Object() { reference.ConditionName }), &H53F) { _
                            .PropertyName = (MyBase.GetFullPropertyName(manager) & ".ConditionName") _
                        }
                        errors.Add(error5)
                    End If
                Next
                Return errors
            End If
            Dim item As New ValidationError(String.Format(CultureInfo.CurrentCulture, Messages.InvalidConditionName, New Object() { "ConditionName" }), &H540) { _
                .PropertyName = (MyBase.GetFullPropertyName(manager) & ".ConditionName") _
            }
            errors.Add(item)
            Return errors
        End Function

    End Class
End Namespace

